package com.koron.css2.clientCenter.impl;

import com.koron.css2.clientCenter.Enum.ProcessStateEnum;
import com.koron.css2.clientCenter.bean.CtmInfoBean;
import com.koron.css2.clientCenter.bean.UserInfoMergeDetail;
import com.koron.css2.clientCenter.bean.UserInfoSplit;
import com.koron.css2.clientCenter.bean.UserInfoSplitDetail;
import com.koron.css2.clientCenter.mapper.*;
import com.koron.css2.clientCenter.queryBean.UserInfoSplitDetailQuery;
import com.koron.css2.clientCenter.queryBean.UserInfoSplitQuery;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.workflow.AbstractProcessInterface;
import com.koron.css2.workflow.ProcessInterface;
import com.koron.util.Constant;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.ArrayList;
import java.util.List;

public class UserInfoSplitProcess extends AbstractProcessInterface {
    @Override
    public MessageBean<?> before(SessionFactory factory, UserInfoBean userInfo, String billId, String fromNodeCode, int operation) {
        try {
            UserInfoSplitMapper userInfoSplitMapper = factory.getMapper(UserInfoSplitMapper.class);
            UserInfoSplitDetailMapper userInfoSplitDetailMapper = factory.getMapper(UserInfoSplitDetailMapper.class);
            UserInfoMapper userInfoMapper = factory.getMapper(UserInfoMapper.class);
            CtmInfoMapper ctmInfoMapper = factory.getMapper(CtmInfoMapper.class);
            AccountInfoMapper accountInfoMapper = factory.getMapper(AccountInfoMapper.class);

            // 1. 通过&&是审批阶段通过
            if (operation == ProcessInterface.OPERATION_AGREE && fromNodeCode.equals(ProcessStateEnum.PROCESSING.getState())) {
                // 2. 根据id查询拆户数据
                UserInfoSplitQuery userInfoSplitQuery = new UserInfoSplitQuery();
                userInfoSplitQuery.setId(billId);
                UserInfoSplit userInfoSplit = userInfoSplitMapper.getSingle(userInfoSplitQuery);
                // 3. 查询拆户详情
                UserInfoSplitDetailQuery userInfoSplitDetailQuery = new UserInfoSplitDetailQuery();
                userInfoSplitDetailQuery.setSplitCode(userInfoSplit.getBillNo());
                List<UserInfoSplitDetail> userInfoSplitDetailList = userInfoSplitDetailMapper.getList(userInfoSplitDetailQuery);
                // 4. user_info表：根据userNo批量更新更新ctmNo，accountNo，ctmName，ctmAddr, certNo,certType,likeTel,mobile,faxNumber
                List<UserInfoMergeDetail> userInfoMergeDetailList = new ArrayList<>();
                for (UserInfoSplitDetail userInfoSplitDetail : userInfoSplitDetailList) {
                    UserInfoMergeDetail userInfoMergeDetail = new UserInfoMergeDetail();
                    // 4.1 userNo,ctmNo，accountNo，ctmName，ctmAddr
                    userInfoMergeDetail.setUserNo(userInfoSplitDetail.getUserNo());
                    userInfoMergeDetail.setCtmNo(userInfoSplitDetail.getCtmNo());
                    userInfoMergeDetail.setAccountNo(userInfoSplitDetail.getAccountNo());
                    userInfoMergeDetail.setCtmName(userInfoSplitDetail.getCtmName());
                    userInfoMergeDetail.setCtmAddr(userInfoSplitDetail.getCtmAddr());
                    // 4.2 certNo,certType,likeTel,mobile,faxNumber
                    CtmInfoBean ctmInfoBean = ctmInfoMapper.getCtmInfoByCtmNo(userInfoSplitDetail.getCtmNo(), CtmInfoBean.CTM);
                    userInfoMergeDetail.setCertNo(ctmInfoBean.getCertNo());
                    userInfoMergeDetail.setCertType(ctmInfoBean.getCertType());
                    userInfoMergeDetail.setLinkTel(ctmInfoBean.getLinkTel());
                    userInfoMergeDetail.setMobile(ctmInfoBean.getMobile());
                    userInfoMergeDetail.setFaxNumber(ctmInfoBean.getFaxNumber());
                    userInfoMergeDetailList.add(userInfoMergeDetail);
                }
                // 4.3 批量更新userInfo
                userInfoMapper.batchUpdateCtmInfoAndAccountNo(userInfoMergeDetailList);
                // 5. 更新user_account表中的ctm_no和account_no的关系
                accountInfoMapper.batchUpdateCtmNoByAccountNo(userInfoMergeDetailList);
            }
            return MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "拆户提交成功", void.class);
        } catch (Exception e) {
            logger.error("拆户提交失败", e);
            return MessageBean.create(Constant.MESSAGE_INT_FAIL, "拆户提交失败", null);
        }
    }
}
